package cn.teleus.common.util.i18n;

import cn.teleus.common.util.Root;
import cn.teleus.common.util.lang.Strings;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * i18n util
 *
 */
public class I18nService implements ApplicationContextAware {
    private ApplicationContext applicationContext;
    /**
     * language type, map
     */
    private static Map<I18nType,Properties> languageMessageMap = new HashMap<>();


    /**
     * 默认中文
     */
    private I18nType defaultLanguage = I18nType.CN;

    @PostConstruct
    public void afterPropertiesSet() throws Exception {
        try {
            for (I18nType i18nType : I18nType.values()){
                String suffix = "_"+i18nType.type;

                String i18nFile = "i18n/message"+suffix+".properties";
                // load prop
                Resource resource = new ClassPathResource(i18nFile);
                if (resource.exists()){
                    EncodedResource encodedResource = new EncodedResource(resource,"UTF-8");
                    Properties prop = PropertiesLoaderUtils.loadProperties(encodedResource);
                    languageMessageMap.put(i18nType,prop);
                }
            }
            //如果不为空，则设置国际化配置
            try {
                I18nService i18nService = applicationContext.getBean(I18nService.class);
                if (i18nService != null){
                    Root.setI18nService(i18nService);
                }
            }catch (BeansException beansException){
                throw new Exception("I18N bean not exist");
            }

        } catch (IOException e) {
            throw new Exception("I18N files not exist");
        }
    }

    public void setDefaultLanguage(I18nType defaultLanguage) {
        this.defaultLanguage = defaultLanguage;
    }

    /**
     * 通过key获取所有配置的语言
     * @param key
     * @return
     */
    public I18nMessage getMessages(String key) {
        Map<I18nType,String> messageMap = new HashMap<>();
        if (!languageMessageMap.isEmpty()){
            languageMessageMap.forEach((type, languageProp) ->{
                if (languageProp.get(key) != null){
                    messageMap.put(type,languageProp.get(key).toString());
                }
            });
        }
        return new I18nMessage(key,messageMap);
    }

    /**
     * 通过key获取默认语言
     * @param key
     * @return
     */
    public String getDefaultMessage(String key) {
        Properties languageProp = languageMessageMap.get(defaultLanguage);
        return languageProp.getProperty(key);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
